﻿using System;

namespace UniRx
{
	// Token: 0x02000223 RID: 547
	public class CountNotifier : IObservable<CountChangedStatus>
	{
		// Token: 0x06000CF5 RID: 3317 RVA: 0x0003E9DA File Offset: 0x0003CDDA
		public CountNotifier(int max = 2147483647)
		{
			if (max <= 0)
			{
				throw new ArgumentException("max");
			}
			this.max = max;
		}

		// Token: 0x17000106 RID: 262
		// (get) Token: 0x06000CF6 RID: 3318 RVA: 0x0003EA11 File Offset: 0x0003CE11
		public int Max
		{
			get
			{
				return this.max;
			}
		}

		// Token: 0x17000107 RID: 263
		// (get) Token: 0x06000CF7 RID: 3319 RVA: 0x0003EA19 File Offset: 0x0003CE19
		// (set) Token: 0x06000CF8 RID: 3320 RVA: 0x0003EA21 File Offset: 0x0003CE21
		public int Count { get; private set; }

		// Token: 0x06000CF9 RID: 3321 RVA: 0x0003EA2C File Offset: 0x0003CE2C
		public IDisposable Increment(int incrementCount = 1)
		{
			if (incrementCount < 0)
			{
				throw new ArgumentException("incrementCount");
			}
			object obj = this.lockObject;
			IDisposable result;
			lock (obj)
			{
				if (this.Count == this.Max)
				{
					result = Disposable.Empty;
				}
				else
				{
					if (incrementCount + this.Count > this.Max)
					{
						this.Count = this.Max;
					}
					else
					{
						this.Count += incrementCount;
					}
					this.statusChanged.OnNext(CountChangedStatus.Increment);
					if (this.Count == this.Max)
					{
						this.statusChanged.OnNext(CountChangedStatus.Max);
					}
					result = Disposable.Create(delegate
					{
						this.Decrement(incrementCount);
					});
				}
			}
			return result;
		}

		// Token: 0x06000CFA RID: 3322 RVA: 0x0003EB24 File Offset: 0x0003CF24
		public void Decrement(int decrementCount = 1)
		{
			if (decrementCount < 0)
			{
				throw new ArgumentException("decrementCount");
			}
			object obj = this.lockObject;
			lock (obj)
			{
				if (this.Count != 0)
				{
					if (this.Count - decrementCount < 0)
					{
						this.Count = 0;
					}
					else
					{
						this.Count -= decrementCount;
					}
					this.statusChanged.OnNext(CountChangedStatus.Decrement);
					if (this.Count == 0)
					{
						this.statusChanged.OnNext(CountChangedStatus.Empty);
					}
				}
			}
		}

		// Token: 0x06000CFB RID: 3323 RVA: 0x0003EBC8 File Offset: 0x0003CFC8
		public IDisposable Subscribe(IObserver<CountChangedStatus> observer)
		{
			return this.statusChanged.Subscribe(observer);
		}

		// Token: 0x0400074F RID: 1871
		private readonly object lockObject = new object();

		// Token: 0x04000750 RID: 1872
		private readonly Subject<CountChangedStatus> statusChanged = new Subject<CountChangedStatus>();

		// Token: 0x04000751 RID: 1873
		private readonly int max;
	}
}
